﻿$(function () {
    $("#signupsubmit").click(function () {
        SubmitSignup();
    });

    $("#form").keypress(function (event) {
        if (event.which == '13') {
            SubmitSignup();
        }
    });

    $("#captcharefresh").click(function () {
        var randomnumber = Math.floor(Math.random() * 9999999)
        $("#captcha").attr("src", "/Security/Captcha?r=" + randomnumber);
    });
});

function SubmitSignup() {

    $("#signupsubmit").hide();

    var input = {
        FirstName: $("#FirstName").val(),
        LastName: $("#LastName").val(),
        Email: $("#Email").val(),
        Street: $("#Street").val(),
        Street2: $("#Street2").val(),
        City: $("#City").val(),
        State: $("#State").val(),
        ZipCode: $("#ZipCode").val(),
        Phone: $("#Phone").val(),
        Password1: $("#Password1").val(),
        Password2: $("#Password2").val(),
        Captcha: $("#Captcha").val()
    };

    Ajax({
        url: "/Security/TrySignup",
        data: JSON.stringify(input),
        success: function (data) {
            if (data.IsValid == true) {
                location.href = "/Security/SignupComplete";
            } else {
                ShowValidationErrors(data);
            }
        },
        complete: function () {
            $("#signupsubmit").show();
        }
    });
}
